package com.vay.de.dang.online.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.vay.de.dang.online.util.UpgradeHelper;
import com.vay.de.dang.online.web.BaseApi;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.ui.HomeActivity;
import com.vay.de.dang.online.util.VnHandler;
import com.vay.de.dang.online.phone.InstallAppHelper;

import java.io.File;

public class DialogUpgrade extends DialogBase {

    private TextView versionTv;
    private TextView contentTv;
    private LinearLayout progressLayout;
    private LinearLayout buttonLayout;
    private String downUrl;
    private TextView progressTv;
    private ProgressBar progressBar;
    private int isGoogleUpdate;

    public DialogUpgrade(Context context) {
        super(context);
        setContentView(R.layout.dialog_version_upgrade);
        autoWidth(0.88f);
        versionTv = findViewById(R.id.update_title);
        contentTv = findViewById(R.id.update_content);
        progressLayout = findViewById(R.id.progress_layout);
        buttonLayout = findViewById(R.id.button_layout);
        progressTv = findViewById(R.id.progress_tv);
        progressBar = findViewById(R.id.progress_bar);

        findViewById(R.id.bnt_no).setOnClickListener(view -> dismiss());
        findViewById(R.id.bnt_yes).setOnClickListener(view -> {

            if (isGoogleUpdate == 1) {
                gotoGoogleMarket();
            } else {
                progressLayout.setVisibility(View.VISIBLE);
                buttonLayout.setVisibility(View.GONE);

                UpgradeHelper.downloadApk(downUrl, new UpgradeHelper.DownloadListener() {

                    @Override
                    public void callback(String url, long totalSize, long readSize) {
                        float r = (float) readSize / totalSize;
                        int pv = (int) (r * 100);

                        progressTv.setText(pv + "%");
                        progressBar.setProgress(pv);
                    }

                    @Override
                    public void complete(File file) {
                        dismiss();
                        InstallAppHelper.installApk(HomeActivity.instance(),
                                "com.vay.de.dang.online.fileProvider", file);
                    }
                });
            }
        });
    }

    public void showUpgrade(int versionUpdate, int googleUpdate, String title, String content, String url) {
        BaseApi.log("升级提醒: versionUpdate=" + versionUpdate
                + "; content=" + content
                + "; googleUpdate=" + googleUpdate
                + "; url=" + url);

        // versionUpdate: 1 强制更新， 0. 非强制更新
        if (versionUpdate == 1) {
            setCancelable(false);
            setCanceledOnTouchOutside(false);
            findViewById(R.id.bnt_no).setVisibility(View.GONE);
        } else {
            setCancelable(true);
            setCanceledOnTouchOutside(true);
        }

        versionTv.setText(title);
        contentTv.setText(content);
        downUrl = url;
        isGoogleUpdate = googleUpdate;

        show();
    }

    public void gotoGoogleMarket() {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + getContext().getPackageName()));
            intent.setPackage("com.android.vending");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            getContext().startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            VnHandler.showText("No Google Market");
        }

        dismiss();
    }
}
